home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / UGroupList.cp < prev    next >
Encoding:
Text File  |  1994-02-20  |  4.4 KB  |  197 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // UGroupList.cp
  3.  
  4. #include "UGroupList.h"
  5. #include "UPrefsDatabase.h"
  6. #include "Tools.h"
  7. #include "StreamTools.h"
  8.  
  9. #include <RsrcGlobals.h>
  10. #include <ErrorGlobals.h>
  11.  
  12.  
  13. #pragma segment MyGroupList
  14.  
  15. const long kCurrentGroupListVersion = 2;
  16. const long kMinGroupListVersion = 1;
  17.  
  18. const short kMaxNameLen = 223;
  19. typedef struct GroupEntry
  20. {
  21.     NewArticleStat fStat;
  22.     char fFiller[31];
  23.     char fName[kMaxNameLen + 1]; // C string
  24. };
  25. typedef GroupEntry *GroupEntryPtr;
  26.  
  27. TGroupList::TGroupList()
  28. {
  29. }
  30.  
  31. pascal void TGroupList::Initialize()
  32. {
  33. #if qDebug
  34.     if (sizeof(GroupEntry) != 256)
  35.         ProgramBreak("sizeof(GroupEntry) != 256");
  36. #endif
  37.     inherited::Initialize();
  38.     fDoc = nil;
  39. }
  40.  
  41. void TGroupList::IGroupList(TDocument *doc)
  42. {
  43.     inherited::IDynamicArray(0, sizeof(GroupEntry));
  44.     fDoc = doc;
  45. }
  46.  
  47. pascal void TGroupList::Free()
  48. {
  49.     inherited::Free();
  50. }
  51.  
  52. void TGroupList::DoRead(TStream *aStream)
  53. {
  54.     long version = aStream->ReadLong();
  55.     MyStreamCheckVersion(version, kMinGroupListVersion, kCurrentGroupListVersion, "TGroupList");
  56.     if (version == 1)
  57.     {
  58.         DoIronAgeFormatRead(aStream);
  59.         return;
  60.     }
  61.     ReadDynamicArray(aStream, this);
  62.     DoPostRead();
  63. }
  64.  
  65. void TGroupList::DoIronAgeFormatRead(TStream *aStream)
  66. {
  67.     aStream->ReadLong(); // fillers
  68.     aStream->ReadLong();
  69.     aStream->ReadLong();
  70.     long size = aStream->ReadLong();
  71.     SetArraySize(size);
  72.     fSize = size;
  73.     if (fSize)
  74.     {
  75.         Boolean prevLock = Lock(true);
  76.         aStream->ReadBytes(ComputeAddress(1), fSize * sizeof(GroupEntry));
  77.         Lock(prevLock);
  78.     }
  79.     DoPostRead();
  80. }
  81.  
  82. void TGroupList::DoWrite(TStream *aStream)
  83. {
  84.     aStream->WriteLong(kCurrentGroupListVersion);
  85.     WriteDynamicArray(aStream, this);
  86. }
  87.  
  88. void TGroupList::DoNeedDiskSpace(long &dataForkBytes)
  89. {
  90.     dataForkBytes += sizeof(long); // version number
  91.     dataForkBytes += MyStreamSizeOfDynamicArray(this);
  92. }
  93.  
  94. void TGroupList::DoPostRead()
  95. {
  96.     for (ArrayIndex index = 1; index <= fSize; index++)
  97.         ComputeGroupEntryAddress(index)->fStat = kUnknown;
  98. }
  99. //======================================================================
  100. GroupEntry *TGroupList::ComputeGroupEntryAddress(ArrayIndex index)
  101. {
  102.     return GroupEntryPtr(inherited::ComputeAddress(index));
  103. }
  104.  
  105. void TGroupList::GetGroupAt(ArrayIndex index, CStr255 &name)
  106. {
  107.     name = ComputeGroupEntryAddress(index)->fName;
  108. }
  109.  
  110. ArrayIndex TGroupList::FindIndexFromName(const CStr255 &name)
  111. {
  112.     for (ArrayIndex index = 1; index <= fSize; index++)
  113.         if (name == ComputeGroupEntryAddress(index)->fName)
  114.             return index;
  115.     return kEmptyIndex;
  116. }
  117.  
  118. NewArticleStat TGroupList::GetNewArticleStat(ArrayIndex index)
  119. {
  120.     return ComputeGroupEntryAddress(index)->fStat;
  121. }
  122.  
  123. void TGroupList::SetNewArticleStat(ArrayIndex index, NewArticleStat newStat)
  124. {
  125.     ComputeGroupEntryAddress(index)->fStat = newStat;
  126. }
  127.  
  128. Boolean TGroupList::HasGroup(const CStr255 &name)
  129. {
  130.     return FindIndexFromName(name) != kEmptyIndex;
  131. }
  132.  
  133. Boolean TGroupList::InsertGroupBefore(ArrayIndex index, const CStr255 &name)
  134. {
  135.     if (FindIndexFromName(name) != kEmptyIndex)
  136.         return false; // in list
  137.     GroupEntry entry;
  138.     BlockSet(Ptr(&entry), sizeof(entry), 0);
  139.     short len = short(Min(kMaxNameLen, name.Length()));
  140.     const unsigned char *p = (const unsigned char*) &name;
  141.     entry.fName[len] = 0;
  142.     BytesMove(p + 1, entry.fName, len);
  143.     InsertElementsBefore(index, &entry, 1);
  144.     if (fDoc)
  145.         fDoc->Changed(cGroupListChange, nil);
  146.     return true;
  147. }
  148.  
  149. void TGroupList::DeleteGroupAt(ArrayIndex index)
  150. {
  151.     DeleteElementsAt(index, 1);
  152.     if (fDoc)
  153.         fDoc->Changed(cGroupListChange, nil);
  154. }
  155.  
  156. void TGroupList::DeleteGroup(const CStr255 &name)
  157. {
  158.     ArrayIndex index = FindIndexFromName(name);
  159.     if (index != kEmptyIndex)
  160.         DeleteGroupAt(index);
  161. }
  162.  
  163. ArrayIndex TGroupList::AddGroupsFromListBefore(ArrayIndex beforeIndex, TGroupList *groupList)
  164. {
  165.     short noGroupsInserted = 0;
  166.     CArrayIterator iter(groupList);
  167.     for (ArrayIndex index = iter.FirstIndex(); iter.More(); index = iter.NextIndex())
  168.     {
  169.         CStr255 name;
  170.         name = groupList->ComputeGroupEntryAddress(index)->fName;
  171.         if (InsertGroupBefore(beforeIndex, name))
  172.         {
  173.             beforeIndex++;
  174.             noGroupsInserted++;
  175.         }
  176.     }
  177.     return noGroupsInserted;
  178. }
  179.  
  180. Boolean TGroupList::AppendGroup(const CStr255 &name)
  181. {
  182.     return InsertGroupBefore(fSize + 1, name);
  183. }
  184.  
  185. Boolean TGroupList::HasOneOfTheGroupsInList(TGroupList *groupList)
  186. {
  187.     CArrayIterator iter(groupList);
  188.     for (ArrayIndex index = iter.FirstIndex(); iter.More(); index = iter.NextIndex())
  189.     {
  190.         CStr255 name;
  191.         groupList->GetGroupAt(index, name);
  192.         if (HasGroup(name))
  193.             return true;
  194.     }
  195.     return false;    
  196. }
  197.